#------------------------
# Summary for analysis
# Date: 2024-04-05
#------------------------

library(data.table)
library(ggplot2)
library(ggsci)
library(survival)
library(survminer)

# Load the df
load("./Data/analysis.Rdata")
df <- df[semster <= 18, ]

# Incidence
## By sex
incidece_sems <- df[,
    .(  # nolint
        incidence = sum(event) / sum(diffDate), persons = .N, # nolint
        personyear = sum(diffDate)
    ),  # nolint
    by = .(semster, sex)
]
incidece_sems[order(semster, sex), ]
## bar-plot
ggplot(aes(semster, incidence, fill = sex), data = incidece_sems) +
    geom_bar(stat = "identity", position = "dodge") + # nolint: indentation_linter
    scale_fill_jama() +
    ylim(0, 0.4) +
    scale_x_continuous(breaks = seq(2, 18, 2)) +
    theme_classic() +
    theme(text = element_text(family = "serif", size = 12))
# nolint: indentation_linter
ggsave("./Plots/incidence_by_sex.jpg", dpi = 300)

## By section
# Survival
survfit <- survfit(Surv(time, event) ~ sex, data = df)
summary(survfit)
survplot <- ggsurvplot(survfit, data = df,
  palette = c("#023048", "#FB8502"),
  pval = TRUE, surv.median.line = "hv",
  risk.table = "abs_pct",
  tables.height = 0.12,
  conf.int = TRUE,
  legend.title = ""
)
survplot
jpeg(filename = "./Plots/sruvival_by_sex.jpg", res = 300, height = 4000,
     width = 5000)
print(survplot, newpage = FALSE)
dev.off()

survfit <- survfit(Surv(time, event) ~ section, data = df)
summary(survfit)
survplot <- ggsurvplot(survfit, data = df,
  #palette = c("#023048", "#FB8502"),
  pval = TRUE, surv.median.line = "hv",
  #risk.table = "abs_pct",
  tables.height = 0.12,
  conf.int = TRUE,
  legend.title = ""
)
survplot
jpeg(filename = "./Plots/sruvival_by_section.jpg", res = 300, height = 4000,
     width = 5000)
print(survplot, newpage = FALSE)
dev.off()
